//获取应用实例
const app = getApp()
const api = require('../../../api/index.js');
var util = require("../../../utils/util.js")
Page({
  calling: function (event) {
    var phone;
    var data = event.currentTarget.dataset.phone;
    util.phone(data, this.data.phone);
    App.zhuge.track('联系Di仔', {});
  },
  /**
   * 页面的初始数据
   */
  data: {
    userData: {},
    Loing:false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.Logins();
    let token = wx.getStorageSync("token").token
    console.log(token,'看看有没有token')
    if (token == undefined || token == null || token == null){
      this.setData({
        Loing: true
      })
    }
    if (!token){
      console.log('iftoken')
      this.setData({
        Loing: true
      })
    }
  },
  getPhoneNumber:function(e){

    let sessionKeyS = wx.getStorageSync('session_key').session_key;
    console.log(sessionKeyS);
    if (sessionKeyS == '' || sessionKeyS == undefined) {
      app.Logins();
    }
    let invite_id = wx.getStorageSync('invite_id');
    let data = {
      encryptedData: e.detail.encryptedData,
      iv: e.detail.iv,
      sessionKey: sessionKeyS,
      invite_id: invite_id
    }
    console.log(data);
    api.getPhoneNumber(data,(res)=>{
      if (res.data.code == 50003){
          this.setData({
            Loing: true
          })
          wx.showToast({
            title: '亲，登录需要授权哦~',
            icon: 'none',
            duration: 2000
          })
      }else{
        this.setData({
          userData: res.data.result,
          Loing: false
        })
        app.globalData.Refresh = true;
        wx.setStorageSync('token', {
          "token": res.data.result.token,
          "user_mobile": res.data.result.user_mobile
        });
        console.log(wx.getStorageSync('token'))
      }
     
      
    })
 },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let tokens = wx.getStorageSync("token").token;
    this.getUserData()
    App.zhuge.track('我的页面', {})
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  },
  getUserData: function(){
    api.my((res) => {
      console.log(res.data.code)
      if (res.data.code == 50001) {
        this.setData({
          Loing: true
        })
        app.Logins();
        console.log('我走到50001了')
      }
      this.setData({
        userData: res.data.result
      }) 
      if (res.data.result.user_id){
        this.setData({
         Loing: false
       })
      }
    })
  }
})